<?php

/**
 * EmsFeaturesBlock form.
 *
 * @package    ems1
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class EmsFeaturesBlockForm extends BaseEmsFeaturesBlockForm
{
    public function configure()
    {
        unset (
            $this['created_at'], $this['updated_at'], $this['element_id']
        );

        $this->widgetSchema['render_title'] = new sfWidgetFormChoice(array(
            'choices'  => Doctrine_Core::getTable('EmsFeaturesBlock')->getBooleanOptions(),
            'multiple' => false,
            'expanded' => true,
        ));

        $this->validatorSchema['render_title'] = new sfValidatorChoice(array(
            'choices' => array_keys(Doctrine_Core::getTable('EmsFeaturesBlock')->getBooleanOptions()),
        ));

        $this->widgetSchema['uses_bullet'] = new sfWidgetFormChoice(array(
            'choices'  => Doctrine_Core::getTable('EmsFeaturesBlock')->getBooleanOptions(),
            'multiple' => false,
            'expanded' => true,
        ));

        $this->validatorSchema['uses_bullet'] = new sfValidatorChoice(array(
            'choices' => array_keys(Doctrine_Core::getTable('EmsFeaturesBlock')->getBooleanOptions()),
        ));

        $this->setWidget('bullet', new sfWidgetFormInputHidden());

        $this->setValidator('bullet',new sfValidatorString(array('required' => false)));

        $this->setWidget('bullet_type', new sfWidgetFormInputHidden());

        $this->setValidator('bullet_type',new sfValidatorString(array('max_length' => 100, 'required' => false)));

        $this->setWidget('bullet_size', new sfWidgetFormInputHidden());

        $this->setValidator('bullet_size',new sfValidatorInteger(array('required' => false)));

        $this->setWidget('tmp_bullet', new sfWidgetFormInputFile());

        $this->widgetSchema->setHelp('tmp_bullet','Debe cargar un archivo de formato .jpg y de dimensiones 30x30 pixeles.');

        $this->setValidator('tmp_bullet',new sfValidatorString(array('required' => false)));

        $this->setWidget('bullet_new', new sfWidgetFormInputHidden());

        $this->setValidator('bullet_new',new sfValidatorString(array('required' => false)));

        $this->setWidget('bullet_new_type', new sfWidgetFormInputHidden());

        $this->setValidator('bullet_new_type',new sfValidatorString(array('max_length' => 100, 'required' => false)));

        $this->setWidget('bullet_new_size', new sfWidgetFormInputHidden());

        $this->setValidator('bullet_new_size',new sfValidatorInteger(array('required' => false)));

        $this->setWidget('tmp_bullet_new', new sfWidgetFormInputFile());

        $this->widgetSchema->setHelp('tmp_bullet_new','Debe cargar un archivo de formato .jpg y de dimensiones 60x30 pixeles.');

        $this->setValidator('tmp_bullet_new',new sfValidatorString(array('required' => false)));

        $folder = sfConfig::get('sf_upload_dir').'/portal_'.$this->getOption('portal_id').'/images/';

        if($this->getObject()->exists() && $this->getObject()->getBullet()){
            $image = $this->getObject()->getBullet();

            if(is_file($folder.$image)){
                $image_size = filesize($folder.$image);

                $this->setDefault('bullet_size',$image_size);
            } else {
                $this->setDefault('bullet_size',0);
            }
        }

        if($this->getObject()->exists() && $this->getObject()->getBulletNew()){
            $image = $this->getObject()->getBulletNew();

            if(is_file($folder.$image)){
                $image_size = filesize($folder.$image);

                $this->setDefault('bullet_new_size',$image_size);
            } else {
                $this->setDefault('bullet_new_size',0);
            }
        }
        
        $this->widgetSchema->setLabels(array(
            'render_title'   => 'Presentar con titulo',
            'uses_bullet'    => 'Utiliza Viñetas',
            'tmp_bullet'     => 'Viñeta',
            'tmp_bullet_new' => 'Viñeta para nuevos',
        ));

        $features = $this->getObject()->getFeatures();

        if(!$features->count()){
            $features = array();
            $feature = new EmsFeature();
            $this->getObject()->getFeatures()->add($feature);
            $features= array($feature);
        }

        $features_forms = new sfForm();
        foreach ($features as $i=>$feature){
            $feature_form = new EmsFeatureForm($feature, array('portal_id' => $this->getOption('portal_id'), 'feature_num' => $i));
            //Embedding each form in the container
            $features_forms->embedForm($i, $feature_form);
        }

        //Embedding the container in the main form
        $this->embedForm('Features', $features_forms);
    }

    public function addFeature($num)
    {
        $feature = new EmsFeature();
        $this->getObject()->getFeatures()->add($feature, $num);
        $feature_form = new EmsFeatureForm($feature, array('portal_id' => $this->getOption('portal_id'), 'feature_num' => $num));
        $this->embeddedForms['Features']->embedForm($num, $feature_form);
        $this->embedForm('Features', $this->embeddedForms['Features']);
    }

    protected function doUpdateObject($values)
    {
        $folder = sfConfig::get('sf_upload_dir').'/portal_'.$this->getOption('portal_id');

        if(strcasecmp($values['bullet'],'') != 0){
            $path_parts_bullet = pathinfo($values['bullet']);

            if(!$this->getObject()->exists() || (strcasecmp($path_parts_bullet['basename'], $this->getObject()->getBullet()) != 0)){
                   $values['bullet'] = $this->saveUploadedfile(
                                    $path_parts_bullet['basename'],
                                    $values['bullet_type'],
                                    $values['bullet'],
                                    $values['bullet_size'],
                                    $folder.'/images/'
                                );
            }
        }

        if(strcasecmp($values['bullet_new'],'') != 0){
            $path_parts_bullet_new = pathinfo($values['bullet_new']);

            if(!$this->getObject()->exists() || (strcasecmp($path_parts_bullet_new['basename'], $this->getObject()->getBulletNew()) != 0)){
                   $values['bullet_new'] = $this->saveUploadedfile(
                                    $path_parts_bullet_new['basename'],
                                    $values['bullet_new_type'],
                                    $values['bullet_new'],
                                    $values['bullet_new_size'],
                                    $folder.'/images/'
                                );
            }
        }

        unset($values['bullet_type'], $values['bullet_size'], $values['tmp_bullet'],
            $values['bullet_new_type'], $values['bullet_new_size'], $values['tmp_bullet_new']
        );

        parent::doUpdateObject($values);
    }

    protected function saveUploadedfile($originalName, $type, $tempName, $size, $path) {

        $validated_file = new sfValidatedFile($originalName, $type, $tempName, $size, $path);

        return $validated_file->save();
    }
}
